package com.lenin.metro.shared;

import java.io.Serializable;

public class MapCoordinates implements Serializable {

	private long latitude;
	private long longitude;
	
	public MapCoordinates() {
		
	}
	
	
	public MapCoordinates(long latitude, long longitude) {
		this.latitude = latitude;
		this.longitude = longitude;
	}

	
	public long getLatitude() {
		return latitude;
	}


	public void setLatitude(long latitude) {
		this.latitude = latitude;
	}

	
	public long getLongitude() {
		return longitude;
	}

	
	public void setLongitude(long longitude) {
		this.longitude = longitude;
	}

	
	public static ProjectionPoint coordsToPoint(MapCoordinates coords) {
		
		long lat = coords.getLatitude();
		long lng = coords.getLongitude();
		
		int x = - (int)(lat - lng);
		int y = (int)(lat + lng) / 2;
		
		return new ProjectionPoint(x, y);
		
	}
	
	
	public static MapCoordinates pointToCoords(ProjectionPoint point) {
		
		long x = point.getX(); // -lat + lng
		long y = point.getY(); // (lat + lng) / 2
		
		// x + y = -lat + lng + ( (lat + lng) / 2 )
		// x + 2y = -lat + lng + lat + lng
		// x + 2y = 2lng
		
		long lng = (x + (2*y)) / 2;
		long lat = - (x - lng);
		
		//System.out.println("("+x+","+y+") -> ["+lat+","+lng+"]");
		
		return new MapCoordinates(lat, lng);
		
	}
	
	
	@Override
	public boolean equals(Object object) {
		
		if(object instanceof MapCoordinates) {
			MapCoordinates coords = (MapCoordinates)object;
			if(coords.getLatitude() == getLatitude() && coords.getLongitude() == getLongitude())
				return true;
		}
		
		return false;
		
	}
	
}
